package android_codebehind;

//import java.awt.Component;
//import java.awt.Dimension;
//import java.awt.GridBagConstraints;

//import javax.imageio.ImageIO;
//import javax.swing.BorderFactory;
//import javax.swing.ImageIcon;
//import javax.swing.border.Border;


import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android_board.GUIBoard;
import dk.dtu.compute.course02312.matadorfinal.MainActivity;
//import android.graphics.Color;

/**
 * Used to create basic android elements
 * 
 * @author Ronnie
 */
public class AndroidComponentFactory {

	// /**
	// * Creates a Dimension object (used to set size on swing components)
	// * @param width - in px
	// * @param height - in px
	// * @return a Dimension object
	// */
	// private Dimension createDimension(int width, int height) {
	// return new Dimension(width, height);
	// }
	// /**
	// * Creats a GridBagConstraints object (used to set the relative position
	// of a swing component on
	// * its parent component. (Don't fortget to set GridBagLayout on parent
	// component.
	// * @param x - in px - direction: left to right
	// * @param y - in px - direction: top to bottom
	// * @param width - in columns
	// * @param height - in rows
	// * @return a new GridBagConstraints object
	// */
	// public GridBagConstraints createGridBagConstraints(int x, int y, int
	// width, int height) {
	// GridBagConstraints gridBagConstraints = new
	// java.awt.GridBagConstraints();
	// gridBagConstraints.gridx = x;
	// gridBagConstraints.gridy = y;
	// gridBagConstraints.gridwidth = width;
	// gridBagConstraints.gridheight = height;
	// return gridBagConstraints;
	// }
	// /**
	// * Overloaded method Calls createGridBagConstraints(x, y, 1, 1)
	// * @param x The X-coordinate
	// * @param y The Y-coordinate
	// * @return a new GridBagConstraints object
	// */
	// public GridBagConstraints createGridBagConstraints(int x, int y) {
	// return createGridBagConstraints(x, y, 1, 1);
	// }
	// /**
	// * Creates an icon using a picture file
	// * @param path - absolute path - start with / to set path relative to
	// project
	// * @return an ImageIcon
	// */
	// public ImageIcon createIcon(String path) {
	// return new ImageIcon(getClass().getResource(path));
	// }
	// /**
	// * Sets the size of a component. Short for calling setMaximumSize,
	// setMinimimSize and
	// * setPreferredSize all by creating single Dimension object using
	// createDimension
	// * @param component - any swing component
	// * @param width - in px
	// * @param height - in px
	// * @return the given component, now with a new size
	// */
	// public Component setSize(Component component, int width, int height) {
	// component.setMaximumSize(createDimension(width, height));
	// component.setMinimumSize(createDimension(width, height));
	// component.setPreferredSize(createDimension(width, height));
	// return component;
	// }
	/**
	 * Creates an image from a source
	 * 
	 * @param src
	 *            The source
	 * @return A new image object
	 */
	public Bitmap createImage(Integer src) {
		Bitmap image = BitmapFactory.decodeResource(
				MainActivity.context.getResources(), src.intValue());
		return image;
	}
	
	static public void createLineBorder(RelativeLayout cell, int thickness, int color) {
		AndroidComponentFactory.createDashedBorder(cell, thickness, 10, color, color);
	}
	
	@SuppressLint("NewApi") @SuppressWarnings("deprecation") 
	static public void createDashedBorder(RelativeLayout cell, int thickness, int stroke, int color1, int color2) {
		final String TAG1 = "border1", TAG2 = "border2", CELLSIZE = "cellsize";
		TextView size = (TextView) cell.findViewWithTag(CELLSIZE);
		int height = GUIBoard.FIELDHEIGHT;
		int width = GUIBoard.FIELDWIDTH;
		if (size != null) {
//			height = size.getHeight(); returnerer ikke det man har sat i setHeight.
//			height = size.getMinimumHeight(); returnerer ikke det man har sat i setHeight.
			height = size.getMinHeight(); // Min og Max er ens.
			width = size.getMinWidth();
		}
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
		LinearLayout lineBorder, dashBorder;
		
		lineBorder = (LinearLayout) cell.findViewWithTag(TAG1);
		dashBorder = (LinearLayout) cell.findViewWithTag(TAG2);
		
		if(lineBorder == null || dashBorder == null){
			lineBorder = new LinearLayout(MainActivity.context);
			dashBorder = new LinearLayout(MainActivity.context);
			lineBorder.setTag(TAG1);
			dashBorder.setTag(TAG2);
			cell.addView(lineBorder, params);
			cell.addView(dashBorder, params);
		}

		ShapeDrawable border1 = new ShapeDrawable(new RectShape());
		Paint paint1 = border1.getPaint();
		paint1.setColor(color1);
		paint1.setStyle(Style.STROKE);
		paint1.setStrokeWidth(thickness);
		// deprecated from api level 16, this uses api level 14!
		lineBorder.setBackgroundDrawable(border1);
		
		ShapeDrawable border2 = new ShapeDrawable(new RectShape());
		Paint paint2 = border2.getPaint();
		paint2.setColor(color2);
		paint2.setStyle(Style.STROKE);
		paint2.setStrokeWidth(thickness);
		paint2.setPathEffect(new DashPathEffect(new float[] {stroke, stroke}, 0));
		dashBorder.setBackgroundDrawable(border2);
	}

//	static public Drawable createDashedBorder(int thickness, int stroke, Color color1,
//			Color color2) {
//		ShapeDrawable border = new ShapeDrawable(new RectShape());
//		Paint paint = border.getPaint();
//		paint.setColor(color1.getRGB());
//		
//		paint.setStyle(Style.STROKE);
//		paint.setStrokeWidth(thickness);
//		
//		return border;
////		int w = stroke * 2;
////		int h = stroke * 2;
////		Bitmap i = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
////		for (int y = 0; y < h; y++) {
////			for (int x = 0; x < w; x++) {
////				if (x < stroke && y < stroke) {
////					i.setPixel(x, y, color1.getRGB());
////				} else if (x >= stroke && y < stroke) {
////					i.setPixel(x, y, color2.getRGB());
////				} else if (x < stroke && y >= stroke) {
////					i.setPixel(x, y, color2.getRGB());
////				} else if (x >= stroke && y >= stroke) {
////					i.setPixel(x, y, color1.getRGB());
////				}
////			}
////		}
////		return BorderFactory.createMatteBorder(thickness, thickness, thickness,
////				thickness, new ImageIcon(i));
//	}
}